

package com.maoshi.shop.coupon.api.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.maoshi.shop.bean.app.dto.CouponDto;
import com.maoshi.shop.bean.app.dto.ProductDto;
import com.maoshi.shop.bean.enums.CouponProdType;
import com.maoshi.shop.common.exception.MaoshiShopBindException;
import com.maoshi.shop.common.util.PageParam;
import com.maoshi.shop.coupon.comment.model.Coupon;
import com.maoshi.shop.coupon.comment.model.CouponProd;
import com.maoshi.shop.coupon.comment.service.CouponService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import ma.glasnost.orika.MapperFacade;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/coupon")
@Api(tags = "优惠券接口")
@AllArgsConstructor
public class CouponController {

    private CouponService couponService;

    private MapperFacade mapperFacade;

    @GetMapping("/listShopCoupon")
    @ApiOperation(value = "查看一店铺的所有优惠券", notes = "通过店铺id(shopId)获取该店优惠券")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "shopId", value = "店铺ID", required = true, dataType = "Long")
    })
    public ResponseEntity<List<CouponDto>> listShopCoupon(@RequestParam(value = "shopId") Long shopId) {
        List<Coupon> couponList = couponService.listPutonByShopId(shopId);
        List<CouponDto> couponDtos = mapperFacade.mapAsList(couponList, CouponDto.class);
        return ResponseEntity.ok(couponDtos);
    }

    @GetMapping("/listByProdId")
    @ApiOperation(value = "获取商品可用的优惠券列表", notes = "获取该商品可用的优惠券列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "shopId", value = "店铺id", required = true),
            @ApiImplicitParam(name = "prodId", value = "商品id", required = true)
    })
    public ResponseEntity<List<CouponDto>> listByProdId(Long shopId, Long prodId) {
        // 获取已投放优惠券
        List<Coupon> couponList = couponService.listPutonByShopId(shopId);
        // 过滤商品
        couponList = couponList.stream().filter(a -> {
            boolean flag = true;
            //过滤指定商品不参与
            if (Objects.equals(a.getSuitableProdType(), CouponProdType.PROD_NO_IN.value())) {
                for (CouponProd couponProd : a.getCouponProds()) {
                    if (Objects.equals(couponProd.getProdId(), prodId)) {
                        flag = false;
                        break;
                    }
                }
            }
            //过滤指定商品参与
            if (Objects.equals(a.getSuitableProdType(), CouponProdType.PROD_IN.value())) {
                flag = false;
                for (CouponProd couponProd : a.getCouponProds()) {
                    if (Objects.equals(couponProd.getProdId(), prodId)) {
                        flag = true;
                        break;
                    }
                }
            }
            return flag;
        }).collect(Collectors.toList());

        List<CouponDto> couponDtos = mapperFacade.mapAsList(couponList, CouponDto.class);

        return ResponseEntity.ok(couponDtos);
    }

    @GetMapping("/prodListByCouponId")
    @ApiOperation(value = "获取优惠券可用的商品列表", notes = "获取优惠券可用的商品列表")
    @ApiImplicitParam(name = "couponId", value = "优惠券id", required = true)
    public ResponseEntity<IPage<ProductDto>> prodListByCouponId(PageParam<ProductDto> page, @RequestParam("couponId") Long couponId) {
        if (couponId <=0) {
            page.setRecords(Collections.emptyList());
            return ResponseEntity.ok(page);
        }
        Coupon coupon = couponService.getById(couponId);
        if (Objects.isNull(coupon) || coupon.getOverdueStatus() != 1) {
            page.setRecords(Collections.emptyList());
            return ResponseEntity.ok(page);
        }

        IPage<ProductDto> productDtoIPage = couponService.prodListByCoupon(page, coupon);
        return ResponseEntity.ok(productDtoIPage);
    }

    @GetMapping("/couponScorePage")
    @ApiOperation(value = "积分优惠券列表", notes = "获取积分商城可换的优惠券列表")
    @ApiImplicitParams({
    })
    public ResponseEntity<IPage<Coupon>> couponScorePage(PageParam<Coupon> page) {
        IPage<Coupon> couponDtos = couponService.page(page,new LambdaQueryWrapper<Coupon>()
            .eq(Coupon::getOverdueStatus,1)
            .eq(Coupon::getPutonStatus, 1)
            .eq(Coupon::getIsScoreType,1));
        return ResponseEntity.ok(couponDtos);
    }

    @GetMapping("/couponScoreById")
    @ApiOperation(value = "获取一条积分优惠券信息", notes = "通过id获取对应积分优惠券")
    @ApiImplicitParam(name = "couponId", value = "优惠券id", required = true)
    public ResponseEntity<Coupon> getCouponScoreById(Long couponId) {
        Coupon coupon = couponService.getById(couponId);
        return ResponseEntity.ok(coupon);
    }

    @GetMapping("/getCouponList")
    @ApiOperation(value = "获取优惠卷列表", notes = "获取 领优惠卷 的优惠券列表")
    public ResponseEntity<List<CouponDto>> getCouponList() {
        List<CouponDto> couponDto = couponService.getCouponList();
        return ResponseEntity.ok(couponDto);
    }

}
